package com.fanqwz.kotlinmvvm.databinding

import android.databinding.BindingAdapter
import android.databinding.DataBindingComponent
import android.databinding.InverseBindingAdapter
import android.widget.TextView


/**
 * Created by fanqwz on 2017/10/31.
 */

// 大坑一个：DataBindingComponent类在dagger gradle插件task之后才会创建，所以不能让dagger直接引用DataBindingComponent类（此时该类没有创建，编译会报NoExistentClass错误）
// https://academy.realm.io/cn/posts/data-binding-android-boyar-mount/ 此处让DataBindingComponent直接作为dagger的component，在项目里无法实现， 除非让dagger注解task放到databinding的task之后（不会，不搞）
interface AppDataBindingComponent : DataBindingComponent {
}

class MyDataBindingComponent : AppDataBindingComponent {
    override fun getIBindingAdapter(): IBindingAdapter {
        return MyBindingAdapter()
    }
}

interface IBindingAdapter {
    @BindingAdapter("android:text")
    fun setText(view: TextView, value: String?)
    @InverseBindingAdapter(attribute = "android:text" , event = "onTextChange")
    fun getText(view: TextView):String
}

class MyBindingAdapter : IBindingAdapter {
    override fun getText(view: TextView): String {
        return "${view.text}"
    }

    override fun setText(view: TextView, value: String?) {
        view.text = "fromDataBinding:${value?.toUpperCase()?: ""}"
    }
}



